package com.example.parkdemo.news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.RadioButton
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.base.user.NewsParam
import com.example.parkdemo.R
import com.example.parkdemo.adapter.NewsAdapter
import com.example.parkdemo.databinding.ActivityNewsBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding, NewsViewModel>() {
    lateinit var newsAdapter: NewsAdapter
    var page = 0
    override fun initData() {
        vm.NewsSuccess.observe(this) {
            val list = Gson().fromJson(it, NewsParam::class.java)
            newsAdapter.data.clear()
            newsAdapter.data.addAll(list)
            newsAdapter.notifyDataSetChanged()
        }
        vm.AddNewsSuccess.observe(this) {
            val map = mutableMapOf<String, Int>()
            map[Const.PARAM_ID] = page
            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)

            vm.News(body)
        }
    }

    override fun initView() {
        binding.newsHeader.headerBack.setOnClickListener { finish() }
        binding.newsHeader.headerTitle.text = "新闻"
        binding.newsHeader.headerAdd.visibility = View.VISIBLE
        binding.newsHeader.headerAdd.setOnClickListener {
            val dialogView = LayoutInflater.from(this).inflate(R.layout.view_add_news, null)
            AlertDialog.Builder(this)
                .setView(dialogView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定") { dialog, which ->
                    val newsTitle =
                        dialogView.findViewById<EditText>(R.id.add_news_title_et).text.toString()
                    val newsValue =
                        dialogView.findViewById<EditText>(R.id.add_news_value_et).text.toString()
                    val newsTime = getTime()
                    var tagId = -1
                    when {
                        dialogView.findViewById<RadioButton>(R.id.add_news_wuye).isChecked -> tagId =
                            0
                        dialogView.findViewById<RadioButton>(R.id.add_news_fun).isChecked -> tagId =
                            1
                        dialogView.findViewById<RadioButton>(R.id.add_news_sport).isChecked -> tagId =
                            2
                        dialogView.findViewById<RadioButton>(R.id.add_news_society).isChecked -> tagId =
                            3
                        dialogView.findViewById<RadioButton>(R.id.add_news_morning).isChecked -> tagId =
                            4

                    }
                    if (newsTitle.isNotEmpty() && newsTime.isNotEmpty() && newsValue.isNotEmpty() && tagId != -1) {
                        val map = mutableMapOf<String, Any>()
                        map[Const.PARAM_ADD_NEWS_TITLE] = newsTitle
                        map[Const.PARAM_ADD_NEWS_TIME] = newsTime
                        map[Const.PARAM_ADD_NEWS_VALUE] = newsValue
                        map[Const.PARAM_ADD_NEWS_TAG_ID] = tagId

                        val json = Gson().toJson(map)
                        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                        vm.addNews(body)
                    }
                }.show()
        }
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_ID] = page
        binding.newsTextTv.text = "第${page + 1}页"
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)

        newsAdapter = NewsAdapter()
        binding.rv.adapter = newsAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)
        vm.News(body)

        binding.newsPreBtn.setOnClickListener {
            if (page == 0) {
                ToastUtils.showLong("已经是第一页了")
            } else {
                page--
                binding.newsTextTv.text = "第${page + 1}页"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_ID] = page

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)

                vm.News(body)
            }
        }
        binding.newsNextBtn.setOnClickListener {
            page++
            binding.newsTextTv.text = "第${page + 1}页"
            val map = mutableMapOf<String, Int>()
            map[Const.PARAM_ID] = page

            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)

            vm.News(body)
        }
    }

}